/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIRegistrarMovimiento.java
 *
 * Created on Oct 20, 2011, 2:26:15 AM
 */
package GUI.Procesamiento;

import BE.BEContacto;
import BE.BELineaMovimiento;
import BE.BEMovimiento;
import BE.BEPallet;
import BE.BEPosicionxRack;
import BE.BERack;
import BL.BLCombo;
import BL.BLModelo;
import BL.BLPallet;
import BL.BLPosicionxrack;
import BL.BLRack;
import BL.BLmovimiento;
import DA.DAContacto;
import GUI.Pallet.GUIMantenimientoPallet;
import GUI.Racks.GUIMantenimientoRack;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import BL.BLCardex;
import BE.BELineaCardex;
import DA.DAPallet;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.Timestamp;

/**
 *
 * @author fenartcos
 */
public class GUIRegistrarTraslado extends javax.swing.JDialog {
    private BEMovimiento objMovimiento;
    private BEContacto objcontacto;
    private List<BELineaMovimiento> lineas=new ArrayList<BELineaMovimiento>();
    private BEPallet objpallet;
    private BEPosicionxRack objPXR;
    private BLCardex blCardex; 

    private BERack objRack;
    /** Creates novew form GUIRegistrarMovimiento */
    public GUIRegistrarTraslado(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
         this.setLocationRelativeTo(null);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        this.setLocation(width/2-this.getWidth()/2, height/2-this.getHeight()/2);
       objMovimiento= new BEMovimiento();
        java.util.Date fecha = new Date();
        BLmovimiento blmov= new BLmovimiento();
        blmov.llenarCombo(cbmTipoMov);
        jDCfecha.setDate(fecha);
        
        //Parametros de la tabla de posiciones
        BLModelo modelo = new BLModelo();
        tblRack.setModel(modelo);

        modelo.addColumn("IDPosicion");
        modelo.addColumn("Fila");
        modelo.addColumn("Columna");
        //Ocultar columna ID
        tblRack.getColumnModel().getColumn(0).setMaxWidth(0);
        tblRack.getColumnModel().getColumn(0).setMinWidth(0);
        tblRack.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
        tblRack.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
        
        blCardex = new BLCardex();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCancelar = new javax.swing.JButton();
        pnlMovimiento = new javax.swing.JPanel();
        lblFecha = new javax.swing.JLabel();
        txtPallet = new javax.swing.JTextField();
        lblCliProv = new javax.swing.JLabel();
        jDCfecha = new com.toedter.calendar.JDateChooser();
        btnAgregar = new javax.swing.JButton();
        lblTipoMov = new javax.swing.JLabel();
        cbmTipoMov = new javax.swing.JComboBox();
        btnAceptar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblRegMov = new javax.swing.JTable();
        btnRemover = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtRack = new javax.swing.JTextField();
        btnrack = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblRack = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registrar Traslado");

        btnCancelar.setText("Cancelar");
        btnCancelar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setMinimumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        pnlMovimiento.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Generales"));

        lblFecha.setText("Fecha:");

        txtPallet.setEnabled(false);
        txtPallet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPalletActionPerformed(evt);
            }
        });

        lblCliProv.setText("Pallet");

        jDCfecha.setEnabled(false);

        btnAgregar.setText("...");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        lblTipoMov.setText("Motivo :");

        cbmTipoMov.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbmTipoMovActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlMovimientoLayout = new javax.swing.GroupLayout(pnlMovimiento);
        pnlMovimiento.setLayout(pnlMovimientoLayout);
        pnlMovimientoLayout.setHorizontalGroup(
            pnlMovimientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMovimientoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMovimientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblCliProv)
                    .addComponent(lblFecha))
                .addGap(31, 31, 31)
                .addGroup(pnlMovimientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnlMovimientoLayout.createSequentialGroup()
                        .addComponent(txtPallet, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnAgregar, 0, 0, Short.MAX_VALUE))
                    .addComponent(jDCfecha, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addComponent(lblTipoMov)
                .addGap(3, 3, 3)
                .addComponent(cbmTipoMov, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnlMovimientoLayout.setVerticalGroup(
            pnlMovimientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMovimientoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMovimientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPallet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblCliProv)
                    .addComponent(btnAgregar)
                    .addComponent(cbmTipoMov, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblTipoMov))
                .addGap(9, 9, 9)
                .addGroup(pnlMovimientoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblFecha)
                    .addComponent(jDCfecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnAceptar.setText("Aceptar");
        btnAceptar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnAceptar.setMinimumSize(new java.awt.Dimension(100, 25));
        btnAceptar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        tblRegMov.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Movimiento", "Linea", "Pallet", "Producto", "Posicion Actual", "Fecha Venc.", "Cantidad"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblRegMov.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        tblRegMov.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblRegMov);
        tblRegMov.getColumnModel().getColumn(0).setMinWidth(0);
        tblRegMov.getColumnModel().getColumn(0).setPreferredWidth(0);
        tblRegMov.getColumnModel().getColumn(0).setMaxWidth(0);
        tblRegMov.getColumnModel().getColumn(1).setMinWidth(0);
        tblRegMov.getColumnModel().getColumn(1).setPreferredWidth(0);
        tblRegMov.getColumnModel().getColumn(1).setMaxWidth(0);
        tblRegMov.getColumnModel().getColumn(5).setMinWidth(0);
        tblRegMov.getColumnModel().getColumn(5).setPreferredWidth(0);
        tblRegMov.getColumnModel().getColumn(5).setMaxWidth(0);

        btnRemover.setText("Remover");
        btnRemover.setMaximumSize(new java.awt.Dimension(100, 25));
        btnRemover.setMinimumSize(new java.awt.Dimension(100, 25));
        btnRemover.setPreferredSize(new java.awt.Dimension(100, 25));
        btnRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoverActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Destino"));

        jLabel1.setText("Rack:");

        txtRack.setEnabled(false);
        txtRack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtRackActionPerformed(evt);
            }
        });

        btnrack.setText("...");
        btnrack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnrackActionPerformed(evt);
            }
        });

        tblRack.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "IDPosicion", "Fila", "Columna"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblRack.setEnabled(false);
        jScrollPane2.setViewportView(tblRack);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(41, 41, 41)
                .addComponent(txtRack, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnrack, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtRack, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnrack)
                        .addComponent(jLabel1))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pnlMovimiento, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnRemover, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnAceptar, btnCancelar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlMovimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnRemover, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtPalletActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPalletActionPerformed

    }//GEN-LAST:event_txtPalletActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        BLCombo idMotivo = (BLCombo)cbmTipoMov.getSelectedItem();
        int fila = 0, columna = 0;
        DAPallet dAPallet = new DAPallet();

        BLPosicionxrack bLPXR=new BLPosicionxrack();
        if (idMotivo.id==1 && txtRack.getText().length()==0){
            JOptionPane.showMessageDialog(null,"Debe seleccionar un Rack",
                                                  "Informacion",JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        if (idMotivo.id==0)
            JOptionPane.showMessageDialog(null,"Debe seleccionar un Motivo Movimiento",
                                                  "Informacion",JOptionPane.INFORMATION_MESSAGE);
        if (txtPallet.getText().length()==0)
        { 
            JOptionPane.showMessageDialog(null,"Debe seleccionar un Pallet",
                                          "Informacion",JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        if (lineas.size()==0)
            JOptionPane.showMessageDialog(null,"Debe agregar un pallet",
                                                  "Informacion",JOptionPane.INFORMATION_MESSAGE);
        else
        {
        //       lineas.get(0).setDescripcion(tblRegMov.getModel().getValueAt(0,5).toString());
            objMovimiento.setContacto(objcontacto);

            objPXR=new BEPosicionxRack();
            BLCombo id = (BLCombo)cbmTipoMov.getSelectedItem();

            BEPosicionxRack objposactual= new BEPosicionxRack();
            objposactual=objpallet.getPosicionxrack();
            
            bLPXR.cambiarEstado(objposactual.getIdposicionxrack(), 0);

            if (id.id==1)
            {
                objpallet.setEstado("UBICADO");
                fila = tblRack.getSelectedRow();
                if (fila < 0)
                {
                    JOptionPane.showMessageDialog(null,"Debe seleccionar una posición para cambiar",
                                                  "Informacion",JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                int idPosicion = Integer.parseInt(tblRack.getModel().getValueAt(fila,columna).toString());
                
                
                objPXR=bLPXR.getById(idPosicion);
                objpallet.setPosicionxrack(objPXR);
                
                //actualizacion de estado y posicion...
                bLPXR.cambiarEstado(objPXR.getIdposicionxrack(), 0);
                BLPallet blpallet = new BLPallet();
                blpallet.cambiarPosicion(objpallet.getIdPallet(), objPXR.getIdposicionxrack());
            }
            else
            {
                fila = tblRegMov.getSelectedRow();
                if (fila < 0)
                {
                    JOptionPane.showMessageDialog(null,"Debe seleccionar un pallet",
                                                  "Información",JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                
                if (!dAPallet.verficaVencimiento(Integer.parseInt(txtPallet.getText())) && id.id == 2){
                    JOptionPane.showMessageDialog(null,"Debe seleccionar un pallet vencido",
                                                  "Informacion",JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                objpallet.setEstado("RETIRADO");
                //objPXR=bLPXR.getById(0);
                objpallet.setPosicionxrack(objPXR);
            }

            objMovimiento.setDetalleMovimiento(lineas);
            objMovimiento.setEstado("1");
            java.util.Date fecha = new Date();
            objMovimiento.setFecha(fecha);
            //objMovimiento.setDescripcion(txtDescripcion.getText());

            idMotivo = (BLCombo)cbmTipoMov.getSelectedItem();
            BLmovimiento blmov = new BLmovimiento();
            objMovimiento.setDestino(objPXR);
            objMovimiento.setOrigen(objposactual);
            
    //        GUIP1BuscarPosiciones dlgP1BuscarPosiciones = new GUIP1BuscarPosiciones(null, true,objpallet);
    //        this.setVisible(false);
    //        dlgP1BuscarPosiciones.setResizable(false);
    //        dlgP1BuscarPosiciones.setVisible(true);
            
            //registro en el cardex:
            BLPosicionxrack objGestorPosRack = new BLPosicionxrack();
            BEPosicionxRack objPosAux = objpallet.getPosicionxrack();
            objGestorPosRack.liberarPosicion(objPosAux);
//            System.out.println("libera posicion");
            BELineaCardex auxLineaCardex = new BELineaCardex();
            
            blCardex.cargarCardexProducto(objpallet.getProducto().getIdProducto());
            auxLineaCardex.setIdProducto(objpallet.getProducto().getIdProducto());

            java.util.Date today = new java.util.Date();
            Timestamp fechaAux = new Timestamp(today.getTime());


            auxLineaCardex.setFecha(fechaAux);
            
            auxLineaCardex.setCantIngreso(0);
            
            auxLineaCardex.set0rigen(objposactual.getIdposicionxrack());
            //auxLineaCardex.setDestino(0);

            auxLineaCardex.setTotalAlmacen(blCardex.obtenerExistencias());

            if (id.id==1){
                //cambio de rack
                auxLineaCardex.setDetalle("Traslado Interno");
                auxLineaCardex.setDestino(objPXR.getIdposicionxrack());
                auxLineaCardex.setCantSalida(0);
                blCardex.guardarTranslado(auxLineaCardex, objpallet.getIdPallet());
                BL.BLLogAudit objGestorLog= new BL.BLLogAudit();
                BE.BELogAudit auxLogAudit= new BE.BELogAudit();
                auxLogAudit.setIdInterface(0);
                auxLogAudit.setIdUsuario(BL.BLCardex.getObjUsuario().getIdUsuario());

                BLRack objGestorRack= new BLRack();


                auxLogAudit.setAccion("Registro de Traslado del Pallet " +objpallet.getIdPallet() +" desde el Rack "+objGestorRack.getRackbyId(objposactual.getIdzona()).getNombre() +" posicion "+objposactual.getFila()+":"+objposactual.getColumna() +"hacia el rack"+ objGestorRack.getRackbyId(objPXR.getIdzona()).getNombre()+" posicion "+objPXR.getFila()+":"+objPXR.getColumna()+" por el usuario "+BL.BLCardex.getObjUsuario().getUsuario()+"." );
                java.util.Date aux= new java.util.Date();
                java.sql.Timestamp auxfecha= new java.sql.Timestamp(aux.getTime());

                auxLogAudit.setFecha(auxfecha);

                objGestorLog.registrarAccion(auxLogAudit);
            }                
            else{
                //vencimiento o rotura de rack
                if (id.id==2)
                {
                    auxLineaCardex.setDetalle("Salida-Vencimiento");
                    
                    
                    BL.BLLogAudit objGestorLog= new BL.BLLogAudit();
                    BE.BELogAudit auxLogAudit= new BE.BELogAudit();
                    auxLogAudit.setIdInterface(0);
                    auxLogAudit.setIdUsuario(BL.BLCardex.getObjUsuario().getIdUsuario());

                    BLRack objGestorRack= new BLRack();


                    auxLogAudit.setAccion("Registro de Salida del Pallet " +objpallet.getIdPallet() +" por Vencimiento del Rack "+objGestorRack.getRackbyId(objposactual.getIdzona()).getNombre() +" posicion "+objposactual.getFila()+":"+objposactual.getColumna() +" por el usuario "+BL.BLCardex.getObjUsuario().getUsuario()+"." );
                    java.util.Date aux= new java.util.Date();
                    java.sql.Timestamp auxfecha= new java.sql.Timestamp(aux.getTime());

                    auxLogAudit.setFecha(auxfecha);

                    objGestorLog.registrarAccion(auxLogAudit);
                    
                }
                if (id.id==3)
                {
                    auxLineaCardex.setDetalle("Salida-Rotura de Stock");
                    BL.BLLogAudit objGestorLog= new BL.BLLogAudit();
                    BE.BELogAudit auxLogAudit= new BE.BELogAudit();
                    auxLogAudit.setIdInterface(0);
                    auxLogAudit.setIdUsuario(BL.BLCardex.getObjUsuario().getIdUsuario());

                    BLRack objGestorRack= new BLRack();


                    auxLogAudit.setAccion("Registro de Salida por Rotura de Stock del Rack "+objGestorRack.getRackbyId(objposactual.getIdzona()).getNombre() +" posicion "+objposactual.getFila()+":"+objposactual.getColumna() +" por el usuario "+BL.BLCardex.getObjUsuario().getUsuario()+"." );
                    java.util.Date aux= new java.util.Date();
                    java.sql.Timestamp auxfecha= new java.sql.Timestamp(aux.getTime());

                    auxLogAudit.setFecha(auxfecha);

                    objGestorLog.registrarAccion(auxLogAudit);
         
                    
                }
                auxLineaCardex.setDestino(0);
                auxLineaCardex.setCantSalida(objpallet.getCantidad());
                blCardex.guardarDespacho(auxLineaCardex, objpallet.getIdPallet());
                
            }
            
            blmov.agregarTraslado(objMovimiento,idMotivo.id);
            
            
             
            
            
            this.dispose();
//
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
    

   if (lineas.size()>0)
        JOptionPane.showMessageDialog(null,"Solo puede agregar un pallet",
                                          "Informacion",JOptionPane.INFORMATION_MESSAGE);
  else  {
        GUIMantenimientoPallet frmAsignarPallet= new GUIMantenimientoPallet(null,true,objcontacto,1);
        frmAsignarPallet.setVisible(true);
        objpallet= new BEPallet();
        objpallet=frmAsignarPallet.getObjPallet();
        

        BELineaMovimiento linea=new BELineaMovimiento();
        
        if (objpallet!=null  && objpallet.getIdPallet()>0){
        objpallet.setEstado("UBICADO");
        linea.setPallet(objpallet);
        txtPallet.setText(objpallet.getIdPallet()+"");
        lineas.add(linea);
       llenarTabla(lineas);


       
        }

        frmAsignarPallet.dispose();

        }
        // TODO add your handling code here:
    }//GEN-LAST:event_btnAgregarActionPerformed

    public void llenarTabla(List <BELineaMovimiento> lista){

   DefaultTableModel modelo=(DefaultTableModel) tblRegMov.getModel();
for (int i=modelo.getRowCount()-1;i>=0;i--){
    modelo.removeRow(i);
}
BEContacto objContacto= new BEContacto();
DAContacto dacontacto= new DAContacto();


if (lista!=null){
    for (int i = 0; i < lista.size(); i++) {
        modelo.addRow(new Object[5]);
        tblRegMov.setValueAt(lista.get(i).getIdLineaMovimiento(), i, 0);

        tblRegMov.setValueAt(lista.get(i).getIdmovimiento(), i, 1);
        tblRegMov.setValueAt(lista.get(i).getPallet().getIdPallet(), i, 2);

 tblRegMov.setValueAt(lista.get(i).getPallet().getProducto().getNombre(), i, 3);
 tblRegMov.setValueAt(lista.get(i).getPallet().getFechaVencimiento().toString(), i, 5);
//objContacto=dacontacto.getContactobyId(lista.get(i).getPallet().getProducto().getIdProveedor());

        tblRegMov.setValueAt(lista.get(i).getPallet().getPosicionxrack().getIdposicionxrack(), i, 4);
        tblRegMov.setValueAt(lista.get(i).getPallet().getCantidad(), i, 6);
    }
    }
}




    private void btnRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoverActionPerformed
       lineas.clear();

      llenarTabla(lineas);

    }//GEN-LAST:event_btnRemoverActionPerformed

    private void txtRackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtRackActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtRackActionPerformed

    public void limpiarTablaPosicion()
    {
        BLModelo modelo = new BLModelo();
        tblRack.setModel(modelo);

        modelo.addColumn("IDPosicion");
        modelo.addColumn("Fila");
        modelo.addColumn("Columna");
        //Ocultar columna ID
        tblRack.getColumnModel().getColumn(0).setMaxWidth(0);
        tblRack.getColumnModel().getColumn(0).setMinWidth(0);
        tblRack.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
        tblRack.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
    }
    
    private void btnrackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnrackActionPerformed
        if (objpallet!=null){
         GUIMantenimientoRack frmRack= new GUIMantenimientoRack(null, true,objpallet.getProducto());
        frmRack.setVisible(true);
        objRack= new BERack();
        objRack=frmRack.getObjrack();

        if (objRack!=null)
        {
            txtRack.setText(objRack.getIdRack() + "");
            BLRack blrack= new BLRack();
            
            tblRack.setEnabled(true);
            List<BEPosicionxRack> tipos = new ArrayList<BEPosicionxRack>();
            tipos = blrack.getPosicionesLibres(objRack.getIdRack());
            this.limpiarTablaPosicion();
        //cmbposicion.setEnabled(true);
          //  blrack.llenarCombo(cmbposicion, objRack.getIdRack());
            BLModelo modelo = new BLModelo();
            tblRack.setModel(modelo);

            modelo.addColumn("IDPosicion");
            modelo.addColumn("Fila");
            modelo.addColumn("Columna");
            //Ocultar columna ID
            tblRack.getColumnModel().getColumn(0).setMaxWidth(0);
            tblRack.getColumnModel().getColumn(0).setMinWidth(0);
            tblRack.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
            tblRack.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
            if (tipos != null)
            for (int i=0;i<tipos.size();i++)
            {
                Object [] fila = new Object[3];
                fila[0] = tipos.get(i).getIdposicionxrack();
                fila[1] = tipos.get(i).getFila();
                fila[2] = tipos.get(i).getColumna();
                modelo.addRow(fila);
            }     
        }


frmRack.dispose();
        }


 else {
            JOptionPane.showMessageDialog(null,"Debe seleccionar un pallet",
                                          "Informacion",JOptionPane.INFORMATION_MESSAGE);
            tblRack.setEnabled(false);
        }
    }//GEN-LAST:event_btnrackActionPerformed

    private void cbmTipoMovActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbmTipoMovActionPerformed
        // TODO add your handling code here:
            BLCombo id = (BLCombo)cbmTipoMov.getSelectedItem();
System.out.println(id.id);
        if (id.id==1)
            btnrack.setEnabled(true);
        else
            btnrack.setEnabled(false);
    }//GEN-LAST:event_cbmTipoMovActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUIRegistrarTraslado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIRegistrarTraslado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIRegistrarTraslado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIRegistrarTraslado.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                GUIRegistrarTraslado dialog = new GUIRegistrarTraslado(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnRemover;
    private javax.swing.JButton btnrack;
    private javax.swing.JComboBox cbmTipoMov;
    private com.toedter.calendar.JDateChooser jDCfecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblCliProv;
    private javax.swing.JLabel lblFecha;
    private javax.swing.JLabel lblTipoMov;
    private javax.swing.JPanel pnlMovimiento;
    private javax.swing.JTable tblRack;
    private javax.swing.JTable tblRegMov;
    private javax.swing.JTextField txtPallet;
    private javax.swing.JTextField txtRack;
    // End of variables declaration//GEN-END:variables
}
